package com.inibb.app.util.type;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

public class BlackToNull {

	private static String NULL_FIELD = null;

	public static void Execute(Object object) {
		loop(object, object.getClass());
	}

	private static void loop(Object object, Class c) {
		Field[] fields = c.getDeclaredFields();
		
		if (object != null) {
			for (Field f : fields) {
				try {
					String str = f.getName();
					String fieldName = str.substring(0, 1).toUpperCase()
							.concat(str.substring(1, str.length()));
					Object objTemp = new Object();

					if (f.getType().equals(String.class)) {
						Method getMethod = c.getMethod("get" + fieldName, null);
						objTemp = getMethod.invoke(object, null);

						if ((String) objTemp == "") {
							Method setMethod = c.getMethod("set" + fieldName,
									String.class);
							setMethod.invoke(object, NULL_FIELD);
						}
					} else if (!f.getType().isPrimitive()
							&& !f.getType().equals(Date.class)) {
						Method getMethod = c.getMethod("get" + fieldName, null);
						objTemp = getMethod.invoke(object, null);

						loop(objTemp, f.getType());
					}
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return;
	}

}
